# script for writing west coast port buffers, includes AK and HI
import arcpy
import os

# allowing for files to be overwritten
arcpy.env.overwriteOutput=True

# spatial reference
spatial_reference = arcpy.SpatialReference(4326) # GCS_WGS_1984

# output files and dir
out_dir = r"H:\My Drive\Boats\ReplicationCode\data"
gdb_name = "port_buffers"
out_name = r"west_coast_ports_eca100_AK_HI_v3"
out_ak_hi = r"AK_HI_ports"
outGDB = os.path.join(out_dir,gdb_name+'.gdb')
outFC = os.path.join(outGDB,out_name)

if not arcpy.Exists(outGDB) :
	arcpy.management.CreateFileGDB(out_dir, gdb_name )

if arcpy.Exists(outFC) : 
    arcpy.Delete_management(outFC)

# lines need to be oriented upward to get right classification of what side the tracks are one
#               ID Lat Long
coordsList = [[1, -122.512 , 37.767], # SF
              [1, -122.535 , 37.828],
              [2, -117.224 , 32.683], # SD (ordering reversed to get right to be inland)
              [2, -117.235 , 32.673],
              [3, -118.24  , 33.712], # POLA (ordering reversed to get right to be inland) 
              [3, -118.257 , 33.706], 
              [4, -118.171 , 33.724],  # POLB (ordering reversed to get right to be inland)  
              [4, -118.19  , 33.723],
              [5, -119.208  , 34.145],# PO Hueneme
              [5, -119.217 , 34.146], 
              [6, -123.955 , 46.176], # Portland, Mouth of Columbia
              [6, -124.051  , 46.297],               
              [7, -124.6 , 48.38], # Seattle, Mouth of Strait of Juan de Fuca
              [7, -124.6 , 48.57],
              [9, -124.139 , 46.906], # Port of Grays Harbor
              [9, -124.18 , 46.926],
              [10, -125.184 , 48.801], # Port Alberni, BC
              [10, -125.469 , 48.926],
              [11, -124.34 , 43.343], # Coos Bay, Oregon
              [11, -124.313 , 43.388],
              [12, -124.26 , 40.729], # Humboldt Bay, CA
              [12, -124.226 , 40.772],
              [13, -116.632 , 31.85], # Ensenada, MX
              [13, -116.619 , 31.85],
              [14, -164.976, 54.126], # Unimak Pass, AK
              [14, -164.857, 54.429],
              [15, -151.977, 59.276], # Cook Inlet, AK
              [15, -152.3, 58.980],
              [16, -146.923, 60.307], # Prince William Sound
              [16, -146.723, 60.376],
              [17, -157.867, 21.299], # Honolulu, HI
              [17, -157.872, 21.301],
              [18, -157.964, 21.309], # Pearl Harbor
              [18, -157.959, 21.311],
              [19, -158.122, 21.321], # Barber's Point Harbor 
              [19, -158.123, 21.323],
              [20, -159.346, 21.953], # Nawiliwili Bay (Kauai)
              [20, -159.343, 21.957],
              [21, -156.471, 20.9], # Kahalui (Maui)
              [21, -156.475, 20.902],
              [22, -155.025, 19.791], # Hilo (Hawaii)
              [22, -155.063, 19.848],              
              [51, -119.797 , 34.417], # SB Channel
              [51, -119.859 , 34.073],
              [52, -122.777 , 37.525], # SF TSS, S
              [52, -122.619 , 37.525],
              [53, -122.896 , 37.713], # SF TSS, W
              [53, -122.835 , 37.608],
              [54, -122.883 , 37.894], # SF TSS, N
              [54, -122.944 , 37.816],
              [55, -118.201 , 33.463], # LA TSS, S
              [55, -118.081 , 33.493],
              [56, -118.323 , 33.675], # LA TSS, W
              [56, -118.248 , 33.546],
              [57, -118.479 , 33.921], # El Segundo Crude Terminal
              [57, -118.45 , 33.899],
              [58, -117.11 , 32.37], # Rosarito Crude Terminal
              [58, -117.08 , 32.327],
              [101,-116.71 , 31.732], # updating extending from southern most NA ECA point to just south of ensenada, mexico
	      [101, -118.605833 , 31.133056 ],
              [102 , -118.605833 , 31.133056 ], # to west of SD
              [102 , -120.742 , 31.9 ], 
              [102 , -121.353 , 32.537 ], 
              [103 , -121.353 , 32.537 ], # west of SD to west of LA
              [103 , -122.051 , 33.043 ],
              [103 , -122.424 , 33.736 ],
              [104 , -122.424 , 33.736 ], # west of LA to west of midpoint to SF
              [104 , -123.823 , 35.749 ],
              [105 , -123.823 , 35.749 ], # midpoint to SF to west of SF
              [105 , -125.279 , 37.805 ],
              [106 , -125.279 , 37.805 ], # west of SF to CA border
              [106 , -126.572 , 40.157 ],
              [106 , -126.621 , 42.000 ],
              [107 , -126.621 , 42.000 ], # CA border to west of portland
              [107 , -126.81 , 42.965 ],
              [107 , -126.450 , 46.014 ], 
              [108 , -126.450 , 46.014 ], #west of portland to west of seattle
              [108 , -127.235 , 48.384 ],
              [109 , -127.235 , 48.384 ], #north of seattle
              [109 , -126.33 , 49.398 ] ,
              [120, -155.868, 20.265], # Alenuihaha Channel, HI
              [120, -156.1, 20.646],
              [121, -157.502, 21.123], # Kaiwi Channel, HI
              [121, -157.666, 21.324],
              [122, -158.265 , 21.587], # Kaieiewaho Channel, HI
              [122, -159.311 , 22.045] ,
              [123, -122.744 , 29.73 ] , # 30km outside of NA ECA 100km box that is parallel to ECA boundary
              [123, -122.02 , 30.375 ] , # should capture traffic coming from Asia to Panama Canal
              [124, -123.507 , 30.328 ] , # northern segment
              [124, -122.763 , 30.955 ] ,
              [125, -119.203 , 28.049  ] , # west of isla guadalupe (125km by 100km)
              [125, -118.327 , 28.863 ] ,  # will capture roughly same vessels as above
              [126, -119.075 , 29.523 ] ,  
              [126, -120.012 , 28.643 ] ,
              [127, -117.2 , 29.771  ] , # east of isla guadalupe (125km by 100km)
              [127, -118.16 , 28.989 ] ,  # will capture vessels from NA ports (not SD and LA/LB)
              [128, -117.789 , 30.517 ] ,  
              [128, -118.724 , 29.731 ] ,
              [130, -117.309 , 30.572 ] ,  # shipping lane south of study area boundary west of baja california
              [130, -116.642 , 30.815 ]  ]


# string names of port buffers
port_names = {  1   : 'Bay' ,
                2   : 'SD' ,
                3   : 'LA' ,
                4   : 'LB' ,
                5   : 'Hue' ,
                6   : 'Port' ,
                7   : 'Sea' ,
                9   : 'GH' ,
                10  : 'PA' ,
                11  : 'CB' ,
                12  : 'HB' ,
                13  : 'Ensenada' ,
                14  : 'Unimak' ,
                15  : 'CookInlet' ,
                16  : 'PWS' ,
                17  : 'Honol' ,
                18  : 'PearHar' ,
                19  : 'BarberPnt' ,
                20  : 'Nawili' ,
                21  : 'Kahalui' ,
                22  : 'Hilo' ,
                51  : "SB" ,
                52  : "SF TSS S" ,
                53  : "SF TSS W" ,
                54  : "SF TSS N" ,
                55  : "LA TSS S" ,
                56  : "LA TSS W" ,
                57  : "El Segundo" ,
                58  : "Rosarito" ,
                101 : 'US 1' ,
                102 : 'US 2' ,
                103 : 'US 3' ,
                104 : 'US 4' ,
                105 : 'US 5',
                106 : 'US 6' ,
                107 : 'US 7' ,
                108 : 'US 8' ,
                109 : 'US 9' ,
                120 : 'Alenuihaha',
                121 : 'Kaiwi' ,
                122 : 'Kaieiewaho' ,
                123 : 'Off_eca_s',
                124 : 'Off_eca_n' ,
                125 : 'Off_Wguad_s' ,
                126 : 'Off_Wguad_n' ,
                127 : 'Off_Eguad_s' ,
                128 : 'Off_Eguad_n',
                129 : 'Off_baja_n' ,
                130 : 'Off_baja_s'          }


arcpy.CreateFeatureclass_management(outGDB, out_name,geometry_type="POLYLINE",spatial_reference=spatial_reference)

# add id field
arcpy.AddField_management (outFC, "PORT_ID", "DOUBLE")


cur = None 
try:

    # Open an insert cursor for the new feature class
    cur = arcpy.da.InsertCursor(outFC, ["SHAPE@","PORT_ID"])

    # Create an array object needed to create features
    array = arcpy.Array()

    # Initialize a variable for keeping track of a feature's ID.
    ID = -1
    for coords in coordsList: 
        if ID == -1:
            ID = coords[0]

        # Add the point to the feature's array of points
        #   If the ID has changed, create a new feature
        if ID != coords[0]:
            cur.insertRow([arcpy.Polyline(array), ID])
            array.removeAll()
        array.add(arcpy.Point(coords[1], coords[2], ID=coords[0]))
        ID = coords[0]

    # Add the last feature
    polyline = arcpy.Polyline(array, spatial_reference)
    cur.insertRow([polyline,ID])


except Exception as e:
   print(e)
finally:
    # Cleanup the cursor if necessary
    if cur:
        del cur

# add field
arcpy.AddField_management (outFC, "PORT_NAME", "TEXT" , 10 )

# loop through lines and add text
with arcpy.da.UpdateCursor(outFC,["PORT_ID","PORT_NAME"]) as cursor:
    for row in cursor :
        row[1] = port_names[row[0]]
        cursor.updateRow(row)
del cursor

### saving layer with just hawaii and AK ports
ak_hi_final = r"in_memory\ak_hi" 
arcpy.MakeFeatureLayer_management(outFC,ak_hi_final,'(PORT_ID=14) OR (PORT_ID=15) OR (PORT_ID=16) OR (PORT_ID=120) OR (PORT_ID=121) OR (PORT_ID=122) ')
arcpy.FeatureClassToFeatureClass_conversion(ak_hi_final, outGDB, out_ak_hi )

